//
// Created by whg on 2025-06-01.
//

#include "pyInteger.hpp"
#include "runtime/global.hpp"

#define COMPARE(x) x ? Global::PyTrue : Global::PyFalse

PyInteger::PyInteger(int i){
    _value = i;
    setKlass(IntegerKlass::getInstance());
}

IntegerKlass* IntegerKlass::instance = nullptr;
IntegerKlass::IntegerKlass() {

}

IntegerKlass* IntegerKlass::getInstance() {
    if(instance == nullptr){
        instance = new IntegerKlass();
    }
    return instance;
}

void IntegerKlass::print(PyObject *obj) {
    PyInteger* intObj = (PyInteger*)obj;
    assert(intObj->klass() == this);
    printf("%d", intObj->value());
}

PyObject *IntegerKlass::add(PyObject *x, PyObject *y) {
    PyInteger* ix = (PyInteger*)x;
    PyInteger* iy = (PyInteger*)y;
    assert(ix->klass() == this);
    assert(iy->klass() == this);
    return new PyInteger(ix->value() + iy->value());
}

PyObject *IntegerKlass::greater(PyObject *x, PyObject *y) {
    PyInteger* ix = (PyInteger*)x;
    PyInteger* iy = (PyInteger*)y;
    assert(ix->klass() == this);
    assert(iy->klass() == this);
    return COMPARE(ix->value() > iy->value());
}

PyObject *IntegerKlass::less(PyObject *x, PyObject *y) {
    PyInteger* ix = (PyInteger*)x;
    PyInteger* iy = (PyInteger*)y;
    assert(ix->klass() == this);
    assert(iy->klass() == this);
    return COMPARE(ix->value() < iy->value());
}

PyObject *IntegerKlass::equal(PyObject *x, PyObject *y) {
    if(x->klass() != y->klass()){
        return Global::PyFalse;
    }
    PyInteger* ix = (PyInteger*)x;
    PyInteger* iy = (PyInteger*)y;
    assert(ix->klass() == this);
    assert(iy->klass() == this);
    return COMPARE(ix->value() == iy->value());
}
